﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters;

namespace QuanLyTruongHoc_App
{
    public partial class QHMonThiTN : Form
    {
        private int state, idNamHoc;
        private Boolean wasSave;
        private MyDataSet.QLTH.tbl_MonHocDataTable tbl_MH;

        public QHMonThiTN()
        {
            InitializeComponent();
        }

        private void checkUserPower()
        {
            int pow = PublicLib.getUserPower(PConst.QUYEN_QL_MON_HOC);
            // user co quyen view
            /*if (!((pow & PConst.POW_VIEW) > 0))
            {
                MessageBox.Show(MSG.MSG_KHONG_DU_QUYEN);
                this.Close();
            }*/
            // user co quyen them
            tsb_TaoMoi.Enabled = ((pow & PConst.POW_ADD) > 0);

            // user co quyen edit
            tsb_Edit.Enabled = ((pow & PConst.POW_EDIT) > 0);

            // user co delete
            dg_DanhSach.AllowUserToDeleteRows =
                tsb_Delete.Enabled = ((pow & PConst.POW_DEL) > 0);

            // --------------------------------
            pow = PublicLib.getUserPower(PConst.QUYEN_QL_MON_THI_TN);
            // edit
            tsb_Save.Enabled = ((pow & PConst.POW_EDIT) > 0);
            dg_DanhSach.ReadOnly = !tsb_Save.Enabled;
        }

        private void QLHinhThucCanBo_Load(object sender, EventArgs e)
        {
            state = PConst.STATE_LOAD;
            grc_maMonHoc.DefaultCellStyle.BackColor =
                grc_tenMonHoc.DefaultCellStyle.BackColor =
                    //grc_HeSo.DefaultCellStyle.BackColor =
                        //grc_DiemHK.DefaultCellStyle.BackColor =
                            //grc_tinhLenLop.DefaultCellStyle.BackColor =
                                cl_STT.DefaultCellStyle.BackColor =
                  Color.LightGray;
            loadCbNamHoc();
            state = PConst.STATE_LOADED;
        }

        private void loadCbNamHoc()
        {
            tbl_NamHocTableAdapter adap = new tbl_NamHocTableAdapter();
            adap.Connection.ConnectionString = PublicLib.getStrConn();
            adap.FillByFlag(qLTH.tbl_NamHoc, PConst.FLAG_NONE);

            tscb_NamHoc.Items.Clear();
            for (int i = 0; i < qLTH.tbl_NamHoc.Rows.Count; i++)
            {
                tscb_NamHoc.Items.Add(qLTH.tbl_NamHoc.Rows[i][qLTH.tbl_NamHoc.TenColumn].ToString());
            }

            if (tscb_NamHoc.Items.Count > 0)
                tscb_NamHoc.SelectedIndex = tscb_NamHoc.Items.Count - 1;
        }

        private void refreshData()
        {
            qh_MonThiTNTableAdapter lopAdap = new qh_MonThiTNTableAdapter();

            tbl_MonHocTableAdapter.Connection.ConnectionString = 
                lopAdap.Connection.ConnectionString = 
                PublicLib.getStrConn();

            tbl_MonHocTableAdapter.ClearBeforeFill = true;

            tbl_MH = new QuanLyTruongHoc_App.MyDataSet.QLTH.tbl_MonHocDataTable();

            this.tbl_MonHocTableAdapter.FillByFlag(tbl_MH, PConst.FLAG_NONE);
            tbl_MH.Columns.Add("Hoc", Type.GetType("System.Boolean"));

            tblMonHocBindingSource.DataSource = tbl_MH;
            grc_Hoc.DataPropertyName = "Hoc";

            if (tscb_NamHoc.SelectedIndex == -1) return;

            idNamHoc = (int)qLTH.tbl_NamHoc.Rows[tscb_NamHoc.SelectedIndex][qLTH.tbl_NamHoc.idColumn];
            lopAdap.FillByNamHoc(qLTH.qh_MonThiTN, idNamHoc);

            for (int i = 0; i < tbl_MH.Rows.Count; i++)
            {
                DataRow[] tmpRow = qLTH.qh_MonThiTN.Select(qLTH.qh_MonThiTN.idMonHocColumn.ColumnName + "=" +
                                                            (int)tbl_MH.Rows[i][tbl_MH.idColumn]);

                if (tmpRow.Length > 0)
                {
                    //dg_DanhSach.Rows[i].Cells[grc_Hoc.Index].Value = true;
                    //if (tmpRow[0][qLTH.qh_MonThiTN.SoTietColumn] != DBNull.Value)
                    //    tbl_MH[i][tbl_MH.SoTietColumn] = tmpRow[0][qLTH.qh_LopMonHoc.SoTietColumn];

                    tbl_MH[i]["Hoc"] = true;
                    //tbl_MH[i]["MonChuyen"] = tmpRow[0][qLTH.qh_LopMonHoc.MonChuyenColumn];
                    //if (tmpRow[0][qLTH.qh_LopMonHoc.HeSoColumn] != DBNull.Value)
                    //    tbl_MH[i][tbl_MH.HeSoColumn] = tmpRow[0][qLTH.qh_LopMonHoc.HeSoColumn];

                    //if (tmpRow[0][qLTH.qh_LopMonHoc.TinhLenLopColumn] != DBNull.Value)
                    //    tbl_MH[i][tbl_MH.TinhLenLopColumn] = tmpRow[0][qLTH.qh_LopMonHoc.TinhLenLopColumn];

                    //if (tmpRow[0][qLTH.qh_LopMonHoc.TinhDiemHKColumn] != DBNull.Value)
                    //    tbl_MH[i][tbl_MH.DungDiemHocKyColumn] = tmpRow[0][qLTH.qh_LopMonHoc.TinhDiemHKColumn];
                }
            }

            for (int i = 0; i < dg_DanhSach.RowCount; i++)
            {
                dg_DanhSach[cl_STT.Index, i].Value = (i + 1);
                dg_DanhSach.Rows[i].DefaultCellStyle.BackColor = ((i % 2 == 1) ? PConst.COLOR_BACK_1 : PConst.COLOR_BACK_DEF );
            }

            wasSave = true;
        }

        private void bt_ThemMoi_Click(object sender, EventArgs e)
        {
            AddEditMonHoc frm_AddMH = new AddEditMonHoc();
            frm_AddMH.MdiParent = this.MdiParent;
            frm_AddMH.Show();
            frm_AddMH.Disposed += new EventHandler(frm_AddMH_Disposed);
        }

        private void bt_Sua_Click(object sender, EventArgs e)
        {
            if (dg_DanhSach.SelectedRows.Count == 0)
                return;
            int i = int.Parse(dg_DanhSach.SelectedRows[0].Cells[grc_id.Name].Value.ToString());
            AddEditMonHoc frm_EditMH = new AddEditMonHoc();
            frm_EditMH.MdiParent = this.MdiParent;
            frm_EditMH.Show();
            frm_EditMH.setEditInfo(i);
            frm_EditMH.Disposed += new EventHandler(frm_AddMH_Disposed);
        }

        private void frm_AddMH_Disposed(object sender, EventArgs e)
        {
            refreshData();
        }

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            if (dg_DanhSach.SelectedRows.Count <= 0)
                return;
            if (MessageBox.Show(MSG.MSG_DELETE, "", MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
            {
                deleteByID(dg_DanhSach.SelectedRows[0].Cells[grc_id.Name].Value.ToString());
            }
        }

        private void deleteByID(String _id)
        {
        
            try
            {
                PublicLib.updateField(qLTH.tbl_MonHoc.TableName,
                                        qLTH.tbl_MonHoc.idColumn.ColumnName,
                                        _id,
                                        qLTH.tbl_MonHoc.flagColumn.ColumnName,
                                        PConst.FLAG_DELETE.ToString());
                refreshData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(MSG.MSG_ERROR_INSERT_DB + " " + ex.Message);
            }
        }

        private void QLLoaiDiem_Resize(object sender, EventArgs e)
        {
            dg_DanhSach.Width = this.Width - 5;
            dg_DanhSach.Height = stts_Bottom.Top - dg_DanhSach.Top - 1;
        }

        private void dg_DanhSach_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            bt_Sua_Click(tsb_Edit, new EventArgs());
        }

        private void saveData()
        {
            if (tscb_NamHoc.SelectedIndex == -1)
                return;

            qh_MonThiTNTableAdapter lopAdap = new qh_MonThiTNTableAdapter();

            lopAdap.Connection.ConnectionString = PublicLib.getStrConn();
            lopAdap.FillByNamHoc(qLTH.qh_MonThiTN, idNamHoc);

            for (int i = 0; i < tbl_MH.Rows.Count; i++)
            {
                DataRow[] tmpRow = qLTH.qh_MonThiTN.Select(qLTH.qh_MonThiTN.idMonHocColumn.ColumnName + "=" +
                                                            (int)tbl_MH.Rows[i][tbl_MH.idColumn]);

                if (tmpRow.Length > 0)
                {
                    if (tbl_MH.Rows[i]["Hoc"] != DBNull.Value && ((Boolean)tbl_MH.Rows[i]["Hoc"]))
                    {
                        //tmpRow[0][qLTH.qh_LopMonHoc.SoTietColumn] = tbl_MH.Rows[i][tbl_MH.SoTietColumn];
                        //tmpRow[0][qLTH.qh_LopMonHoc.TinhLenLopColumn] = tbl_MH.Rows[i][tbl_MH.TinhLenLopColumn];
                        //tmpRow[0][qLTH.qh_LopMonHoc.TinhDiemHKColumn] = tbl_MH.Rows[i][tbl_MH.DungDiemHocKyColumn];
                        //tmpRow[0][qLTH.qh_LopMonHoc.HeSoColumn] = tbl_MH.Rows[i][tbl_MH.HeSoColumn];
                        //tmpRow[0][qLTH.qh_LopMonHoc.MonChuyenColumn] = tbl_MH.Rows[i]["MonChuyen"];
                    }
                    else
                        tmpRow[0].Delete();
                    //lopAdap.Update(tmpRow[0]);
                }
                else
                {
                    if (tbl_MH.Rows[i]["Hoc"] != DBNull.Value)
                    {
                        if ((Boolean)tbl_MH.Rows[i]["Hoc"])
                        {
                            DataRow nRow = qLTH.qh_MonThiTN.NewRow();
                            nRow[qLTH.qh_MonThiTN.idNamHocColumn] = idNamHoc;
                            nRow[qLTH.qh_MonThiTN.idMonHocColumn] = tbl_MH.Rows[i][tbl_MH.idColumn];
                            /*
                            nRow[qLTH.qh_LopMonHoc.SoTietColumn] = tbl_MH.Rows[i][tbl_MH.SoTietColumn];
                            nRow[qLTH.qh_LopMonHoc.TinhLenLopColumn] = tbl_MH.Rows[i][tbl_MH.TinhLenLopColumn];
                            nRow[qLTH.qh_LopMonHoc.TinhDiemHKColumn] = tbl_MH.Rows[i][tbl_MH.DungDiemHocKyColumn];
                            nRow[qLTH.qh_LopMonHoc.HeSoColumn] = tbl_MH.Rows[i][tbl_MH.HeSoColumn];
                            nRow[qLTH.qh_LopMonHoc.MonChuyenColumn] = tbl_MH.Rows[i]["MonChuyen"];
                            */
                            qLTH.qh_MonThiTN.Rows.Add(nRow);
                            //lopAdap.Update(nRow);
                        }
                    }
                }
            }

            lopAdap.Update(qLTH.qh_MonThiTN);

            wasSave = true;
        }

        private void tsb_Save_Click(object sender, EventArgs e)
        {
            dg_DanhSach.EndEdit();
            saveData();
        }

        private void tscb_Lop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (state == PConst.STATE_LOAD)
                return;
            if (!wasSave)
                if (MessageBox.Show(MSG.MSG_SAVE, "", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    saveData();
            refreshData();
        }

        private void dg_DanhSach_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            wasSave = false;
        }

        private void QHLopMonHoc_Shown(object sender, EventArgs e)
        {
            refreshData();
        }

        private void tsb_Reset_Click(object sender, EventArgs e)
        {
            refreshData();
        }

        private void tsb_All_Click(object sender, EventArgs e)
        {
            Boolean val = true;
            for (int i = 0; i < dg_DanhSach.RowCount; i++)
                dg_DanhSach.Rows[i].Cells[grc_Hoc.Index].Value = val;
            wasSave = false;
        }

        private void tblMonHocBindingSource_BindingComplete(object sender, BindingCompleteEventArgs e)
        {
            for (int i = 0; i < dg_DanhSach.RowCount; i++)
            {
                dg_DanhSach[cl_STT.Index, i].Value = (i + 1);
                if (i % 2 == 1)
                    dg_DanhSach.Rows[i].DefaultCellStyle.BackColor = PConst.COLOR_BACK_1;
                else
                    dg_DanhSach.Rows[i].DefaultCellStyle.BackColor = PConst.COLOR_BACK_DEF;
            }
        }

        private void dg_DanhSach_Sorted(object sender, EventArgs e)
        {
            for (int i = 0; i < dg_DanhSach.RowCount; i++)
            {
                dg_DanhSach[cl_STT.Index, i].Value = (i + 1);
                if (i % 2 == 1)
                    dg_DanhSach.Rows[i].DefaultCellStyle.BackColor = PConst.COLOR_BACK_1;
                else
                    dg_DanhSach.Rows[i].DefaultCellStyle.BackColor = PConst.COLOR_BACK_DEF;
            }
        }

        private void QHMonThiTN_Activated(object sender, EventArgs e)
        {
            checkUserPower();
        }

        private void tsb_In_Click(object sender, EventArgs e)
        {
            PrintDataGrid.PrintDGV print = new PrintDataGrid.PrintDGV();
            print.Print_DataGridView(dg_DanhSach);
        }
    }
}
